from db.aiomysql_connector import AioMysqlClient
from services.cache_service import CacheUtils
from services.sql_service.alarm_service import AlarmService
from services.sql_service.plc_service import PLCService
from services.sql_service.production_service import ProductionService
from services.sql_service.station_service import StationService


class DigitTwinService:

    def __init__(self):
        self.sql_client: AioMysqlClient = CacheUtils.get('aiomysql_pool')
        self.station_service: StationService = StationService()
        self.production_service: ProductionService = ProductionService()
        self.alarm_service: AlarmService = AlarmService()
        self.plc_service: PLCService = PLCService()

    async def get_data(self):
        semi_finished_check_result, finished_check_result = await self.station_service.get_check_result()
        quality_monitor = await self.production_service.cur_day_quality_monitor()
        total_quality_monitor = await self.production_service.get_ten_10_total_quality()
        day_product_trend = await self.production_service.day_product_trend()
        alarm_data = await self.alarm_service.alarm_data()
        # 第一次连接返回R1位置机器人数据
        digit_twin_unit = await self.plc_service.r1_robot_datas()
        data = {
            'semi_finished_check_result': semi_finished_check_result,  # 半成品库库位信息
            'finished_check_result': finished_check_result,  # 成品库库位信息
            'quality_monitor': quality_monitor,  # 工位质量监控
            'total_quality_monitor': total_quality_monitor,  # 总线质量监控
            'alarm_data': alarm_data,  # 报警信息
            'day_product_trend': day_product_trend,  # 日生产趋势
            'digit_twin_unit': digit_twin_unit,  # 孪生单元
            'digit_status': {'sn': -1, 'status': -1, },  # 机器人状态

        }

        return data
